import request from '@/utils/request'
import { getLang } from '@/utils/auth'
import store from '@/store'
export function loginByUsername(username, password) {
  const data = {
    czyId: username,
    pwd: password,
    lang: getLang()
  }
  return request({
    url: 'common/login/login',
    method: 'post',
    data
  })
}

export function logout() {
  return request({
    url: 'common/login/loginOut',
    method: 'post'
  })
}

export function getSupportLang() {
  const data = {
    codeTypes: JSON.stringify(['dlyy']),
    lang: 'ALL',
    isAll: 'false'
  }
  return request({
    url: 'common/getOptions',
    method: 'get',
    params: data
  })
}

export function refreshMenuVisit(menuId) {
  const data = {
    menuId: menuId
  }
  return request({
    url: '/common/login/refreshMenuCount',
    method: 'post',
    data
  })
}

export function addShortCut(menuId) {
  const data = {
    menuId: menuId
  }
  return request({
    url: '/common/login/addShortCut',
    method: 'post',
    data
  })
}
export function delShortCut(menuId) {
  const data = {
    menuId: menuId
  }
  return request({
    url: '/common/login/delShortCut',
    method: 'post',
    data
  })
}

export function getMenus() {
  return request({
    url: 'common/login/getMenus',
    method: 'get'
  })
}

export function getRecentOperation() {
  return request({
    url: '/common/login/getRecentOperation',
    method: 'get'
  })
}

export function refreshMenu() {
  return new Promise((resolve, rejects) => {
    store.dispatch('getMenus').then(res => { // 获取菜单
      const menus = res // note:menus is array
      store.dispatch('GenerateRoutes', { menus }).then(response => { // 根据菜单生成路由表
        store.dispatch('setCommonOperations', response).then(() => {
          resolve()
        })
      })
    })
  })
}
